複合類型的結構
C++ 複合類型 是透過將基本類型與一個 類型修飾符結合來定義。雖然參考與指標都提供間接存取,但它們在哲學上具有本質差異。一個 參考 (&) 是一個永久的別名——現有物件的暱稱。一旦綁定,便無法重新指向。相反地,一個 指標 (*) 是記憶體中的一個獨立物件,儲存一個十六進位的 位址。它可以被重新指向不同的物件,或設為 nullptr。
記憶體的視覺化
在程式碼中 int *p1, p2;,只有 p1 是個指標; p2 是個普通的整數。若要讓兩者都是指標,請使用 int *p1, *p2;。這強調了修飾符屬於單獨的宣告子,而非基本類型。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which of the following creates a reference correctly?
int &r;
int i = 10; int &r = i;
int &r = 10;
int *&r = &i;
✅ Correct!
Correct! References must be initialized and bound to an object, not a literal or nothing.❌ Incorrect
References are not objects; they must be bound to an existing variable upon declaration.QUESTION 2
In the declaration 'int* p1, p2;', what are the types of p1 and p2?
Both are pointers to int.
p1 is an int, p2 is a pointer to int.
p1 is a pointer to int, p2 is a plain int.
Neither are valid C++.
✅ Correct!
The '*' modifier applies only to p1. The base type is int, but the declarator p2 lacks the modifier.❌ Incorrect
Type modifiers like '*' or '&' attach to the specific variable name, not the base type (int).QUESTION 3
Which operator is used to obtain the memory address of an object?
The asterisk (*)
The ampersand (&)
The arrow (->)
The dot (.)
✅ Correct!
The address-of operator (&) returns the location of an object in memory.❌ Incorrect
The asterisk (*) is used for dereferencing (accessing the value at an address) or declaration.QUESTION 4
What is the result of dereferencing a pointer?
It gives the memory address.
It creates a new reference.
It yields the object to which the pointer points.
It clears the pointer's memory.
✅ Correct!
Dereferencing 'follows' the address to the actual object, allowing you to read or modify it.❌ Incorrect
Dereferencing retrieves the value, while the address-of operator retrieves the location.QUESTION 5
Which statement about references is FALSE?
A reference is not an object.
A reference can be null.
A reference must be initialized.
A reference cannot be redirected to a new object.
✅ Correct!
References must always be bound to a valid object; there is no such thing as a 'null reference'.❌ Incorrect
References are fixed aliases and do not have a null state, unlike pointers.Module Assessment: Pointers, References, and Sales_data
Applying Compound Types and Structs
You are tasked with upgrading a legacy system. You must manage data using custom structures and ensure memory safety through correct pointer and reference usage.
Q
Exercise 2.41: Based on the Sales_data struct (including bookNo, units_sold, and revenue), how would you rewrite a loop to read multiple transactions and update the total? Focus on the main function logic.
Solution:
To implement Exercise 2.41, define the Sales_data struct at the top of the file. In main(), declare a 'total' object. Use a while loop with 'std::cin >> total.bookNo >> total.units_sold >> price' where revenue is calculated as 'units_sold * price'. For subsequent entries, check if 'trans.bookNo == total.bookNo'. If true, update total.units_sold and total.revenue; if false, print the current total and reset it to the new transaction.
To implement Exercise 2.41, define the Sales_data struct at the top of the file. In main(), declare a 'total' object. Use a while loop with 'std::cin >> total.bookNo >> total.units_sold >> price' where revenue is calculated as 'units_sold * price'. For subsequent entries, check if 'trans.bookNo == total.bookNo'. If true, update total.units_sold and total.revenue; if false, print the current total and reset it to the new transaction.
Q
Consider the following snippet: 'int i = 42; int *p = &i; *p = *p * *p;'. Explain the final value of 'i' and the role of the asterisk in each part of the second line.
Solution:
The final value of 'i' is 1764 (42 squared). In '*p = *p * *p;', the first and second asterisks are dereference operators used to fetch and store values at the address held by p. The third asterisk is the multiplication operator. This demonstrates how pointers allow indirect modification of the original variable 'i'.
The final value of 'i' is 1764 (42 squared). In '*p = *p * *p;', the first and second asterisks are dereference operators used to fetch and store values at the address held by p. The third asterisk is the multiplication operator. This demonstrates how pointers allow indirect modification of the original variable 'i'.